package com.decent.ejfadmin.job;

import com.decent.ejfadmin.common.enums.SupRedisKey;
import com.decent.ejfadmin.common.exception.SupException;
import com.decent.ejfadmin.common.util.Constants;
import com.decent.ejfadmin.finance.service.SupplyFinanceService;
import com.decent.ejfadmin.utils.SimpleCacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 会员提现自动审核定时任务
 *
 * @author HEJUNLEI
 */
@Component
@Configurable
@Slf4j
public class WithdrawAutoAuditJob {
    @Autowired
    private SupplyFinanceService supplyFinanceService;

    /**
     * 每天23:10自动关闭提现接口
     */
    @Scheduled(cron = "0 10 23 1/1 * ?    ")
    private void autoCloseAutoAudit() {
        try {
            supplyFinanceService.changeAutoAuditStatus(Constants.STATE_CLOSE);
        } catch (SupException e) {
            log.warn("自动关闭提现接口异常[{}]", e);
        }
    }

    /**
     * 每天00:40自动打开提现接口
     */
    @Scheduled(cron = "0 40 00 1/1 * ? ")
    public void autoOpenAutoAudit() {
        try {
            Object object = SimpleCacheUtil.getObject(SupRedisKey.AUTO_AUDIT_STOP_KEY.toString());
            if (object == null) {
                supplyFinanceService.changeAutoAuditStatus(Constants.STATE_OPEN);
            } else {
                SimpleCacheUtil.removeObject(SupRedisKey.AUTO_AUDIT_STOP_KEY.toString());
            }
        } catch (SupException e) {
            log.warn("自动打开提现接口异常[{}]", e);
        }
    }


}
